package com.zixuan.gulicollege.service_order.utils;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.zixuan.gulicollege.entity.order.Order;
import com.zixuan.gulicollege.service_order.config.AliPayConfig;
import com.zixuan.gulicollege.service_order.config.AlipayBean;
import io.netty.util.internal.logging.Slf4JLoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @classname: AliPayUtils
 * @author: zixuan
 * @date: 2021/5/28 19:59
 * @description:
 */
public class AliPayUtils {

    private static Logger log = LoggerFactory.getLogger(Slf4JLoggerFactory.class);

    /**
     * 支付宝支付
     * @param alipayBean
     * @param order
     * @return
     * @throws AlipayApiException
     */
    public static String connect(AlipayBean alipayBean, Order order) throws AlipayApiException {
        //1、获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                // 支付宝网关
                AliPayConfig.GATEWAY_URL,
                // appId
                AliPayConfig.APP_ID,
                // 商户私钥
                AliPayConfig.MERCHANT_PRIVATE_KEY,
                // 返回格式
                AliPayConfig.FORMAT,
                // 字符编码格式
                AliPayConfig.CHARSET,
                // 支付宝公钥
                AliPayConfig.ALIPAY_PUBLIC_KEY,
                // 签名方式
                AliPayConfig.SIGN_TYPE
        );
        // 设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        // 页面跳转同步通知页面路径
        alipayRequest.setReturnUrl(AliPayConfig.RETURN_URL + order.getCourseId());
        // 服务器异步通知页面路径
        alipayRequest.setNotifyUrl(AliPayConfig.NOTIFY_URL);

        log.info(JSON.toJSONString(alipayBean));

        //封装参数
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));


        //3、请求支付宝进行付款，并获取支付结果
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        //返回付款信息
        return result;
    }

    /**
     * 将request转为Map
     * @param request
     * @return
     */
    public static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) {

        // 参数Map
        Map<?, ?> properties = request.getParameterMap();
        // 返回值Map
        Map<String, String> returnMap = new HashMap<>(16);
        Iterator<?> entries = properties.entrySet().iterator();

        Map.Entry<String, Object> entry;
        String name = "";
        String value = "";
        Object valueObj =null;
        while (entries.hasNext()) {
            entry = (Map.Entry<String, Object>) entries.next();
            name = (String) entry.getKey();
            valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            } else if (valueObj instanceof String[]) {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) {
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        return returnMap;

    }


}
